//
//  WorkAppInfo.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 2017/8/30.
//  Copyright © 2017年 com.xtion.crm. All rights reserved.
//

#import "WorkAppInfo.h"

@implementation WorkAppInfo

+(XMSQueueDictionary*)constraint:(WorkflowApplyInfo*)workflowApplyInfo
{
    NSArray *fields = workflowApplyInfo.fields;
    BOOL attachmentRequired = workflowApplyInfo.customForm.attachmentRequired;
    
    XMSQueueDictionary *tmp = [XMSQueueDictionary new];
    NSMutableArray *ar = [NSMutableArray new];
    for (NSInteger i = 0; i < fields.count; i ++)
    {
        CustomerFormFieldInfo *field = fields[i];
        FieldDescript *desc = [WorkAppInfo convertCustomerFormFieldInfo:field];
        
        if([desc.controlType intValue] == DynamicFieldControlTypeNone)
        {
            continue;
        }
        
        [workflowApplyInfo.fieldConfigItems enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            FieldConfigItemInfo *item = obj;
            if([item.customForm2FieldID isEqualToString:desc.expandFieldID]){
                switch ([desc.controlType intValue]) {
                    case DynamicFieldControlTypeSingleSelect:
                    {
                        desc.optional = item.value;
                        break;
                    }
                    case DynamicFieldControlTypeSingleSelectDic:
                    {
                        [desc.param setValue:item.value forKey:Dictionary_TypeId];
                        break;
                    }
                    default:
                        break;
                }
                
                *stop = YES;
            }
        }];
        
        [ar addObject:desc];
    }
    
    //attachment
    {
        [ar addObject:[self attachment:attachmentRequired]];
    }
    
    [tmp addKey:@"基本信息" value:ar];
    
    return tmp;
}

+(FieldDescript*)convertCustomerFormFieldInfo:(CustomerFormFieldInfo*)info
{
    FieldDescript *fix = [FieldDescript new];
    fix.controlType = [XMSNumberInt numberIntWithIntValue:[WorkAppInfo convertControlType:info]];
    fix.entityRegID = @"";
    fix.expandFieldID = info.customerFormFieldId;
    fix.fieldLabel = info.dBDescription;
    fix.fieldLength = [XMSNumberInt numberIntWithIntValue:50];
    fix.fieldName = info.fieldName;
    fix.fieldType = [XMSNumberInt numberIntWithIntValue:DynamicFieldTypeCustom];
    if(info.required == YES)
    {
        fix.isAllowEmpty = [XMSNumberInt numberIntWithIntValue:0];
    }
    else
    {
        fix.isAllowEmpty = [XMSNumberInt numberIntWithIntValue:1];
    }
    fix.isReadOnly = [XMSNumberInt numberIntWithIntValue:0];
    fix.optional = @"";
    fix.order = info.sortIndex;
    fix.regx = @"";
    fix.status = nil;
    fix.optIsVisible = [XMSNumberInt numberIntWithIntValue:1];
    fix.viewIsVisible = [XMSNumberInt numberIntWithIntValue:1];
    return fix;
}

+(NSInteger)convertControlType:(CustomerFormFieldInfo*)info
{
    NSInteger controlType = DynamicFieldControlTypeNone;

    //系统字段特殊处理
    if([info.fieldName isEqualToString:SysField_ID]){
        return DynamicFieldControlTypeCreateUUID;
    }
    else if([info.fieldName isEqualToString:SysField_CreateUserID]){
        return DynamicFieldControlTypeCreateUserID;
    }
    else if([info.fieldName isEqualToString:SysField_CreateUserName]){
        return DynamicFieldControlTypeCreateUserName;
    }
    else if([info.fieldName isEqualToString:SysField_CreateDeptID]){
        return DynamicFieldControlTypeCreateDeptID;
    }
    else if([info.fieldName isEqualToString:SysField_CreateDeptName]){
        return DynamicFieldControlTypeCreateDeptName;
    }
    else if([info.fieldName isEqualToString:SysField_CreateDate]){
        return DynamicFieldControlTypeCreateDate;
    }
    
    //controlName
    NSString *controlName = info.controlName;
    if([controlName isEqualToString:Control_Hidden]){
        controlType = DynamicFieldControlTypeHidden;
    }
    else if([controlName isEqualToString:Control_DatePicker]){
        controlType = DynamicFieldControlTypeDate;
    }
    else if([controlName isEqualToString:Control_TextBox]){
        if([info.dBType isEqualToString:DBType_Decimal])
        {
            controlType = DynamicFieldControlTypeDecimal;
        }
        else
        {
            controlType = DynamicFieldControlTypeText;
        }
    }
    else if([controlName isEqualToString:Control_TextArea]){
        controlType = DynamicFieldControlTypeBigText;
    }
    else if([controlName isEqualToString:Control_DropDownList]){
        controlType = DynamicFieldControlTypeSingleSelect;
    }
    else if([controlName isEqualToString:Control_DropDownListFix]){
        controlType = DynamicFieldControlTypeSingleSelect;
    }
    else if([controlName isEqualToString:Control_ZiDianBiaoDropDownList]){
        controlType = DynamicFieldControlTypeSingleSelectDic;
    }
    else if([controlName isEqualToString:Control_Numeric]){
        if([info.dBType isEqualToString:DBType_Decimal])
        {
            controlType = DynamicFieldControlTypeDecimal;
        }
        else
        {
            controlType = DynamicFieldControlTypeInt;
        }
    }
    else if([controlName isEqualToString:Control_SelectDept]){
        controlType = DynamicFieldControlTypeSelectDept;
    }
    else if([controlName isEqualToString:Control_SelectUser]){
        controlType = DynamicFieldControlTypeSelectUser;
    }
    else if([controlName isEqualToString:Control_Attachment]){
        controlType = DynamicFieldControlTypeMixedPanel;
    }
    else if([controlName isEqualToString:Control_SelectDept]){
        controlType = DynamicFieldControlTypeSelectDept;
    }

    return controlType;
}

+(FieldDescript*)attachment:(BOOL)required
{
    FieldDescript *fix = [FieldDescript new];
    fix.controlType = [XMSNumberInt numberIntWithIntValue:DynamicFieldControlTypeMixedPanel];
    fix.entityRegID = @"";
    fix.expandFieldID = @"";
    fix.fieldLabel = @"附件";
    fix.fieldLength = [XMSNumberInt numberIntWithIntValue:50];
    fix.fieldName = Field_Attachment;
    fix.fieldType = [XMSNumberInt numberIntWithIntValue:DynamicFieldTypeCustom];
    if(required == YES)
    {
        fix.isAllowEmpty = [XMSNumberInt numberIntWithIntValue:0];
    }
    else
    {
        fix.isAllowEmpty = [XMSNumberInt numberIntWithIntValue:1];
    }
    fix.isReadOnly = [XMSNumberInt numberIntWithIntValue:0];
    fix.optional = @"";
    fix.order = [XMSNumberInt numberIntWithIntValue:100];
    fix.regx = @"";
    fix.status = nil;
    fix.optIsVisible = [XMSNumberInt numberIntWithIntValue:1];
    fix.viewIsVisible = [XMSNumberInt numberIntWithIntValue:1];
    return fix;
}

@end


@implementation CustomerFormInfo

static XMSQueueDictionary *s_jsonPair_customer_form;

+ (void)initialize
{
    if(s_jsonPair_customer_form == nil)
    {
        XMSQueueDictionary *qd = [[XMSQueueDictionary alloc]initWithStoreClass:[CDBColPropertyPair class]];
        [qd addKey:@"ID" value:@"customerFormId"];
        [qd addKey:@"Name" value:@"name"];
        [qd addKey:@"TableName" value:@"tableName"];
        [qd addKey:@"AttachmentRequired" value:@"attachmentRequired"];
        [qd addKey:@"IndexTemplate" value:@"indexTemplate"];
        [qd addKey:@"CreateTemplate" value:@"createTemplate"];
        [qd addKey:@"EditTemplate" value:@"editTemplate"];
        [qd addKey:@"AppNameJsTemplate" value:@"appNameJsTemplate"];
        [qd addKey:@"CreateDate" value:@"createDate"];
        
        s_jsonPair_customer_form = [CTransformHTTPRESTObj registerJSONObjectClass:[CustomerFormInfo class] JSONKeyPair:qd];
    }
}

+ (XMSQueueDictionary *)JSONObjectProperty:(NSArray *)JSONKeys
{
    return [CTransformHTTPRESTObj JSONObjectMap:s_jsonPair_customer_form propertyWithJSONKey:JSONKeys];
}

@end

@implementation CustomerFormFieldInfo

static XMSQueueDictionary *s_jsonPair_customer_form_field;

+ (void)initialize
{
    if(s_jsonPair_customer_form_field == nil)
    {
        XMSQueueDictionary *qd = [[XMSQueueDictionary alloc]initWithStoreClass:[CDBColPropertyPair class]];
        [qd addKey:@"ID" value:@"customerFormFieldId"];
        [qd addKey:@"CustomForm2ID" value:@"customForm2ID"];
        [qd addKey:@"FieldName" value:@"fieldName"];
        [qd addKey:@"DBType" value:@"dBType"];
        [qd addKey:@"DBDescription" value:@"dBDescription"];
        [qd addKey:@"ControlName" value:@"controlName"];
        [qd addKey:@"SortIndex" value:@"sortIndex"];
        [qd addKey:@"Required" value:@"required"];
        
        s_jsonPair_customer_form_field = [CTransformHTTPRESTObj registerJSONObjectClass:[CustomerFormFieldInfo class] JSONKeyPair:qd];
    }
}

+ (XMSQueueDictionary *)JSONObjectProperty:(NSArray *)JSONKeys
{
    return [CTransformHTTPRESTObj JSONObjectMap:s_jsonPair_customer_form_field propertyWithJSONKey:JSONKeys];
}

@end

@implementation ActivityFieldInfo

static XMSQueueDictionary *s_jsonPair_activity_field;

+ (void)initialize
{
    if(s_jsonPair_activity_field == nil)
    {
        XMSQueueDictionary *qd = [[XMSQueueDictionary alloc]initWithStoreClass:[CDBColPropertyPair class]];
        [qd addKey:@"ID" value:@"activityFieldsId"];
        [qd addKey:@"ProcessGUID" value:@"processGUID"];
        [qd addKey:@"ActivityGUID" value:@"activityGUID"];
        [qd addKey:@"FieldName" value:@"fieldName"];
        [qd addKey:@"Visible" value:@"visible"];
        [qd addKey:@"Editable" value:@"editable"];
        [qd addKey:@"MobileVisible" value:@"mobileVisible"];
        [qd addKey:@"MobileEditable" value:@"mobileEditable"];
        [qd addKey:@"CreateDate" value:@"createDate"];
        
        s_jsonPair_activity_field = [CTransformHTTPRESTObj registerJSONObjectClass:[ActivityFieldInfo class] JSONKeyPair:qd];
    }
}

+ (XMSQueueDictionary *)JSONObjectProperty:(NSArray *)JSONKeys
{
    return [CTransformHTTPRESTObj JSONObjectMap:s_jsonPair_activity_field propertyWithJSONKey:JSONKeys];
}

@end

@implementation FieldConfigItemInfo

static XMSQueueDictionary *s_jsonPair_field_config_item;

+ (void)initialize
{
    if(s_jsonPair_field_config_item == nil)
    {
        XMSQueueDictionary *qd = [[XMSQueueDictionary alloc]initWithStoreClass:[CDBColPropertyPair class]];
        [qd addKey:@"ID" value:@"fieldConfigItemId"];
        [qd addKey:@"CustomForm2FieldID" value:@"customForm2FieldID"];
        [qd addKey:@"Name" value:@"name"];  
        [qd addKey:@"Value" value:@"value"];
        [qd addKey:@"CreateDate" value:@"createDate"];
        
        s_jsonPair_field_config_item = [CTransformHTTPRESTObj registerJSONObjectClass:[FieldConfigItemInfo class] JSONKeyPair:qd];
    }
}

+ (XMSQueueDictionary *)JSONObjectProperty:(NSArray *)JSONKeys
{
    return [CTransformHTTPRESTObj JSONObjectMap:s_jsonPair_field_config_item propertyWithJSONKey:JSONKeys];
}

@end
